package chapter9.section9;

//程序9-12a 利用反射机制分析Account类
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;

public class ClassAnalysis {
	public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException,
			IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		try {
			Class cls = Class.forName("chapter9.section9.Student");
			// 得到cls的父类类型
			Type type = cls.getGenericSuperclass();
			System.out.println("type:" + type);
			// 得到cls的所有实现接口
			Class<Student>[] clses = cls.getInterfaces();
			for (Class<Student> cs : clses) {
				System.out.println(cs);
			}
			// 得到cls的所有构造方法
			Constructor<Student>[] constructors = cls.getDeclaredConstructors();
			for (Constructor<Student> cs : constructors) {
				System.out.println(cs);
			}
//得到cls的方法集合，不包括继承来的
			Method[] ms = cls.getDeclaredMethods();
			for (Method m : ms) {
				System.out.println(m);
			}
			// 得到cls的所有字段，不包括继承来的
			Field[] fs = cls.getDeclaredFields();
			for (Field f : fs) {
				System.out.println(f);
			}

			Object[] paraValue = { "鲁丁", 15 };
			Class[] paraType = { String.class, int.class };

			Constructor<Student> oneConstructor = cls.getDeclaredConstructor(paraType);
			Student s = oneConstructor.newInstance(paraValue);
			System.out.println(s);

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

	}
}
